﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


public partial class RadioButtonList:UserControl
{
    public RadioButtonList()
    {
        // This call is required by the Windows Windows.Forms.Form Designer.
        InitializeComponent();
        // Add any initialization after the InitializeComponent() call.
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    }

    public event System.EventHandler SelectedIndexChanged;

    private object _DataSource;
    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public object DataSource
    {
        get { return _DataSource; }
        set
        {
            _DataSource = value;
            //
            if ((value != null))
            {
                OnDataSourceChanged();
            }
            else
            {
                pnlGroup.Controls.Clear();
            }
        }
    }

    private string _DisplayMember;
    public string DisplayMember
    {
        get { return _DisplayMember; }
        set { _DisplayMember = value; }
    }

    private string _ValueMember;
    public string ValueMember
    {
        get { return _ValueMember; }
        set { _ValueMember = value; }
    }

    private int _ItemWidth;
    [DefaultValue(0)]
    public int ItemWidth
    {
        get { return _ItemWidth; }
        set { _ItemWidth = value; }
    }

    private FlatStyle _FlatStyle;
    [DefaultValue(System.Windows.Forms.FlatStyle.Standard)]
    public System.Windows.Forms.FlatStyle FlatStyle
    {
        get { return _FlatStyle; }
        set { _FlatStyle = value; }
    }

    public FlowDirection FlowDirection
    {
        get { return pnlGroup.FlowDirection; }
        set { pnlGroup.FlowDirection = value; }
    }
    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public object SelectedValue
    {
        get { return GetValue(); }
        set { SetValue(value); }
    }
    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public RadioButton SelectedItem
    {
        get
        {
            foreach (RadioButton opt in pnlGroup.Controls)
            {
                if (opt.Checked)
                    return opt;
            }
            return null;
        }
    }
    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int SelectedIndex
    {
        get
        {
            for (int i = 0; i <= pnlGroup.Controls.Count - 1; i++)
            {
                if (((RadioButton)pnlGroup.Controls[i]).Checked)
                    return i;
            }
            return -1;
        }
        set
        {
            if (value > pnlGroup.Controls.Count - 1)
                throw new Exception("Invalid item index");
            if (value >= 0)
            {
                ((RadioButton)pnlGroup.Controls[value]).Checked = true;
            }
            else
            {
                foreach (RadioButton opt in pnlGroup.Controls)
                {
                    opt.Checked = false;
                }
            }
            if (SelectedIndexChanged != null)
            {
                SelectedIndexChanged(this, new EventArgs());
            }
        }
    }

    public void DataBind(object dataSource)
    {
        if (dataSource is string)
        {
            _DisplayMember = "Item_Value";
            _ValueMember = "Item_Code";
            _DataSource = SystemController.Instance().Domain_GetDic(dataSource.ToString());
        }
        else
        {
            _DataSource = dataSource;
        }
        OnDataSourceChanged();
    }

    public void DataBind(object dataSource, string displayMember, string valueMember)
    {
        _DisplayMember = displayMember;
        _ValueMember = valueMember;
        _DataSource = dataSource;
        OnDataSourceChanged();
    }

    private object GetValue()
    {
        foreach (RadioButton opt in pnlGroup.Controls)
        {
            if (opt.Checked)
                return opt.Tag;
        }
        return null;
    }

    private void SetValue(object value)
    {
        int iIndex = 0;
        if (value == null)
            return;
        foreach (RadioButton opt in pnlGroup.Controls)
        {
            if (Globals.Object_SetValueEx(opt.Tag, value.GetType().Name) == value)
                SelectedIndex = iIndex;
            iIndex += 1;
        }
    }

    private void OnDataSourceChanged()
	{
		pnlGroup.Padding = new Padding(0);
		DataView dt = null;
		if (this.DataSource is DataTable) {
			dt = ((DataTable)this.DataSource).DefaultView;
		} else if (this.DataSource is DataView) {
			dt = this.DataSource as DataView;
		} else if (this.DataSource.GetType().IsArray || object.ReferenceEquals(this.DataSource.GetType().GetGenericTypeDefinition(), typeof(List<>))) {
			pnlGroup.Controls.Clear();

            foreach (var obj_loopVariable in (IEnumerable)(this.DataSource))
            {
                var obj = obj_loopVariable;
                if (obj is ListBoxItem)
                {
                    AddOption(((ListBoxItem)obj).DisplayField, ((ListBoxItem)obj).ValueField);
                }
                else
                {
                    AddOption(obj.ToString(), obj);
                }
            }
		} else {
			throw new Exception("DataSource type not support");
		}
		if ((dt != null)) {
			pnlGroup.Controls.Clear();
			for (int i = 0; i <= dt.Count - 1; i++) {
				AddOption(dt[i][this.DisplayMember].ToString(), dt[i][this.ValueMember]);
			}
		}
		//
		if (m_maxWidth > 0) {
			foreach (RadioButton opt in pnlGroup.Controls) {
				opt.Width = m_maxWidth;
			}
		}
	}
    private int m_maxWidth;
    private void AddOption(string sText, object oValue)
    {
        RadioButton opt = new RadioButton();
        opt.FlatStyle = this.FlatStyle;
        opt.Margin = new Padding(0);
        opt.Text = sText;
        opt.Tag = oValue;
        if (_ItemWidth == 0)
        {
            opt.Width = (int)FormGlobals.App_TextWidth(opt.Text, this.Font) + 30;
            if ((opt.Width > m_maxWidth))
                m_maxWidth = opt.Width;
        }
        else
        {
            opt.Width = _ItemWidth;
        }
        opt.Height = 20;
        opt.CheckedChanged += opt_CheckedChanged;
        pnlGroup.Controls.Add(opt);
    }

    private void opt_CheckedChanged(object sender, EventArgs e)
    {
        if (((RadioButton)sender).Checked)
        {
            SelectedValue = ((RadioButton)sender).Tag;
        }
    }

    protected override void OnAutoSizeChanged(System.EventArgs e)
    {
        pnlGroup.AutoSize = this.AutoSize;
        pnlGroup.AutoSizeMode = this.AutoSizeMode;
        base.OnAutoSizeChanged(e);
    }
}
